﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class ChucNang : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmChucNang";
    static int STT = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int cap = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Title = "Quản lý chức năng | " + Header;
            
            btnCancel_Click(sender, e);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            p = new SqlParameter[5];

            p[0] = new SqlParameter("@ChucNangCha", GetDrop(dlChucNang));
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@TenChucNang", txtTenCN.Text.Trim());
            p[1].SqlDbType = SqlDbType.NVarChar;
            p[2] = new SqlParameter("@Tag", txtTag.Text.Trim().Equals("") ? "#" : txtTag.Text.Trim());
            p[2].SqlDbType = SqlDbType.NVarChar;
            p[3] = new SqlParameter("@ViTri", float.Parse(txtViTri.Text.Trim()));
            p[3].SqlDbType = SqlDbType.Float;
            p[4] = new SqlParameter("@Link", txtLink.Text.Trim().Equals("") ? "#" : txtLink.Text.Trim());
            p[4].SqlDbType = SqlDbType.NVarChar;

            ex.ExecuteNonQuerry("sp_vnttblchucnang_Add", p);
            btnCancel_Click(sender, e);
            gstGetMess("Lưu thành công", "");

        }
        catch (Exception ax)
        {
            gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
            ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tChức Năng\t" + ax.Message);
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtTenCN.Text = "";
        txtTenCN.Focus();
        txtTag.Text = "";
        txtLink.Text = "";
        txtViTri.Text = "";
        dlChucNang.SelectedIndex = 0;
        LoadChucNang(dlChucNang);
        
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    private object GetDrop(DropDownList dl)
    {
        if (dl.SelectedValue.Trim().Equals(""))
            return DBNull.Value;
        else return int.Parse(dl.SelectedValue);
    }
    private void LoadChucNang(DropDownList dl)
    {
        int id1;
        dl.Items.Clear();
        DataTable dt = ex.GetData("sp_vnttblchucnang_ListChucNangCha");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            cap = 0;
            id1 = int.Parse(dt.Rows[i]["IDs"].ToString());
            dl.Items.Add(new ListItem(dt.Rows[i]["TenChucNang"].ToString(), dt.Rows[i]["IDs"].ToString()));
            LoadChild(dlChucNang, id1);
        }
        dl.Items.Insert(0, new ListItem("--Chọn chức năng cha--", ""));
        dl.SelectedIndex = 0;
    }
    private void LoadChild(DropDownList dl2, int id)
    {
        int ids=0;
        
        DataTable dt2=new DataTable();
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", id);
        p[0].SqlDbType = SqlDbType.Int;
        dt2 = ex.GetData("sp_vnttblchucnang_ListChucNangChaByID", p);
        if (dt2.Rows.Count > 0)
        {
            cap++;
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                ids = int.Parse(dt2.Rows[i]["IDs"].ToString());
                if (cap == 1)
                    dl2.Items.Add(new ListItem("-->" + dt2.Rows[i]["TenChucNang"].ToString(), dt2.Rows[i]["IDs"].ToString()));
                else
                    if (cap == 2)
                        dl2.Items.Add(new ListItem("---->" + dt2.Rows[i]["TenChucNang"].ToString(), dt2.Rows[i]["IDs"].ToString()));
                    else dl2.Items.Add(new ListItem("------>" + dt2.Rows[i]["TenChucNang"].ToString(), dt2.Rows[i]["IDs"].ToString()));
                LoadChild(dlChucNang, ids);
            }
            if (id != ids)
            {
                if(cap==2)
                    cap = 1;
                if (cap == 3)
                    cap = 2;
            }
        }
    }
}